﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace WebStudio.Framework.Helper
{
    /// <summary>
    /// 数据类型转换帮助类
    /// </summary>
    public class ConvertHelper
    {
        #region 转换布尔值
        /// <summary>
        /// 转换布尔值
        /// </summary>
        /// <param name="obj">需转换的对象</param>
        /// <returns>布尔值[true或flase]</returns>
        public static bool BoolParse(object obj)
        {
            //初始化
            bool result = false;

            if (obj != null)
            {
                switch (obj.GetType().Name)
                {
                    case "String": result = (obj.ToString().ToLower() == "true") || (obj.ToString().ToLower() == "1"); break;
                    case "Boolean": result = (bool)obj; break;
                    case "Int32": result = ((int)obj) == 1; break;
                }
            }

            //返回
            return result;
        }
        #endregion

        #region 转换整型值
        /// <summary>
        /// 转换整型值
        /// </summary>
        /// <param name="obj">需转换的对象</param>
        /// <returns>整型值</returns>
        public static int IntParse(object obj)
        {
            //初始化
            int result = 0;

            if (obj != null)
            {
                switch (obj.GetType().Name)
                {
                    case "Int32": result = int.Parse(obj.ToString()); break;
                    case "Boolean": result = ((bool)obj) ? 1 : 0; break;
                    default:
                        if (!int.TryParse(obj.ToString(), out result))
                        {
                            result = 0;
                        };
                        break;
                }
            }

            //返回
            return result;
        }

        /// <summary>
        /// 转换整型值
        /// </summary>
        /// <param name="obj">需转换的对象</param>
        /// <param name="value">默认值</param>
        /// <returns>整型值</returns>
        public static int IntParse(object obj, int value)
        {
            //初始化
            int result = value;

            if (obj != null)
            {
                switch (obj.GetType().Name)
                {
                    case "Int32": result = int.Parse(obj.ToString()); break;
                    case "Boolean": result = ((bool)obj) ? 1 : 0; break;
                    default:
                        if (!int.TryParse(obj.ToString(), out result))
                        {
                            result = value;
                        };
                        break;
                }
            }

            //返回
            return result;
        }

        #endregion

        #region 转换十进制值
        /// <summary>
        /// 转换十进制值
        /// </summary>
        /// <param name="obj">需转换的对象</param>
        /// <returns>十进制值</returns>
        public static decimal DecimalParse(object obj)
        {
            //初始化
            decimal result = 0M;

            if (obj != null)
            {
                switch (obj.GetType().Name)
                {
                    case "Int32":
                    case "Int64": result = decimal.Parse(obj.ToString()); break;
                    case "Boolean": result = ((bool)obj) ? 1M : 0M; break;
                    default:
                        if (!decimal.TryParse(obj.ToString(), out result))
                        {
                            result = 0M;
                        };
                        break;
                }
            }

            //返回
            return result;
        }

        #endregion

        #region 转换字符串
        /// <summary>
        /// 转换字符串
        /// </summary>
        /// <param name="obj">需转换的对象</param>
        /// <returns>字符串</returns>
        public static string StringParse(object obj)
        {
            return obj == null ? string.Empty : obj.ToString();
        }
        #endregion

        #region 转换长整型
        /// <summary>
        /// 转换长整型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static long LongParse(object obj)
        {
            long result = 0L;
            if (obj == null)
            {
                return 0L;
            }
            switch (obj.GetType().Name)
            {
                case "Int32":
                    return long.Parse(obj.ToString());

                case "Int64":
                    return long.Parse(obj.ToString());

                case "Boolean":
                    if ((bool)obj)
                    {
                        return 1L;
                    }
                    return 0L;
            }
            string resultString = Regex.Replace(obj.ToString(), "[^0-9]", "");
            if (resultString.Length == 0)
            {
                result = 0L;
            }
            else
            {
                result = long.Parse(resultString);
            }
            if (obj.ToString().StartsWith("-"))
            {
                result *= -1L;
            }
            return result;
        }
        #endregion

        #region 转换时间日期
        /// <summary>
        /// 转换时间日期
        /// </summary>
        /// <param name="obj">需转换的对象</param>
        /// <returns>时间日期</returns>
        public static DateTime DateTimeParse(object obj)
        {
            return obj == null ? Convert.ToDateTime("1900-01-01") : (DateTime)obj;
        }
        #endregion
    }
}
